part of login;

class LoginPanel extends StatefulWidget {
  const LoginPanel({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _LoginPanelState();
}

class _LoginPanelState extends State<LoginPanel> {
  final FocusNode _usernameFocusNode = FocusNode();
  final FocusNode _passwordFocusNode = FocusNode();
  final FocusNode _verifyFocusNode = FocusNode();

  String username = '';
  String password = '';
  String verifyCode = '';

  bool _isSubmitting = false;
  bool _showVerify = false;
  String? imageSrc;

  _doLogin() async {
    if (username.isEmpty) {
      toast('请填写用户名');
      return;
    }
    if (password.isEmpty) {
      toast('请填写密码');
      return;
    }
    //logger.info([username, password]);
    _usernameFocusNode.unfocus();
    _passwordFocusNode.unfocus();
    setState(() {
      _isSubmitting = true;
    });
    final navigator = Navigator.of(context);
    try {
      bool isLogin =
          await app.login.doLogin(username, password, verify: verifyCode);
      if (isLogin) {
        toast('登录成功');
        navigator.pop();
      } else {
        toast('登录失败');
      }
    } on ServerException catch (err) {
      toast(err.message);

      // 显示验证码
      if (err.code == 104) {
        setState(() {
          _showVerify = true;
        });
        getImage();
      }
    } catch (err) {
      toast('登录失败:$err');
    } finally {
      setState(() {
        _isSubmitting = false;
      });
    }
  }

  getImage() async {
    setState(() {
      imageSrc = null;
    });
    String src = await app.login.verifyImage();
    setState(() {
      imageSrc = src;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        const SizedBox(height: 10),
        TextField(
          maxLength: 30,
          focusNode: _usernameFocusNode,
          cursorColor: Colors.white,
          style: const TextStyle(color: Colors.white, height: 1.5),
          strutStyle: const StrutStyle(height: 1.5, fontSize: 16.0),
          textAlignVertical: TextAlignVertical.center,
          decoration: LoginTheme.inputDecoration('手机/会员名', icon: Icons.person),
          onChanged: (String value) {
            username = value;
          },
        ),
        const SizedBox(height: 10),
        TextField(
          maxLength: 30,
          focusNode: _passwordFocusNode,
          style: const TextStyle(color: Colors.white, height: 1.5),
          strutStyle: const StrutStyle(height: 1.5, fontSize: 16.0),
          textAlignVertical: TextAlignVertical.center,
          obscureText: true,
          onChanged: (String value) {
            password = value;
          },
          decoration: LoginTheme.inputDecoration('登录密码', icon: Icons.lock),
        ),
        const SizedBox(height: 10),
        if (_showVerify)
          TextField(
            maxLength: 6,
            focusNode: _verifyFocusNode,
            style: const TextStyle(color: Colors.white, height: 1.5),
            strutStyle: const StrutStyle(height: 1.5, fontSize: 16.0),
            textAlignVertical: TextAlignVertical.center,
            keyboardType: const TextInputType.numberWithOptions(),
            onChanged: (String value) {
              verifyCode = value;
            },
            decoration: LoginTheme.inputDecoration(
              '验证码',
              icon: Icons.check_circle,
              action: GestureDetector(
                onTap: getImage,
                child: Tools.image(imageSrc),
              ),
            ),
          ),
        if (_showVerify) const SizedBox(height: 10),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 20),
          child: StateButton(
            onPressed: _doLogin,
            text: '登录',
            isLoading: _isSubmitting,
          ),
        ),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child: Row(
            children: <Widget>[
              TextButton(
                onPressed: () {
                  _IndexPageState.of(context)
                      .changePanel('register', direction: 'lr');
                },
                child: Text(
                  '注册账号',
                  style: LoginTheme.linkStyle,
                ),
              ),
              const Spacer(),
              TextButton(
                onPressed: () {
                  _IndexPageState.of(context).changePanel('forgot');
                },
                child: Text('忘记密码', style: LoginTheme.linkStyle),
              ),
            ],
          ),
        )
      ],
    );
  }
}
